#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import pygal
import math
from itertools import groupby


def read_json():
    file_name = "btc_close_2017.json"

    btc_data = None
    with open(file_name) as f:
        btc_data = json.load(f)

    dates = []
    months = []
    weeks = []
    weekdays = []
    closes = []
    for btc_dict in btc_data:
        dates.append(btc_dict.get("date"))
        months.append(int(btc_dict.get("month")))
        weeks.append(int(btc_dict.get("week")))
        weekdays.append(btc_dict.get("weekday"))
        closes.append(int(float(btc_dict.get("close"))))
    return dates, months, weeks, weekdays, closes


def btc_json():
    """ svg
    """
    json_result = read_json()
    dates = json_result[0]
    closes = json_result[4]

    line_char = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
    line_char.title = '收盘价1'
    line_char.x_labels = dates

    N = 20
    line_char.x_labels_major = dates[::N]
    line_char.add('收盘价', closes)
    line_char.render_to_file('收盘价折线图.svg')


def btc_json_log10():
    """ 半对数变换
    """
    json_result = read_json()
    dates = json_result[0]
    closes = json_result[4]

    line_char = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
    line_char.title = '收盘价1'
    line_char.x_labels = dates

    N = 20
    line_char.x_labels_major = dates[::N]

    close_log = [math.log10(_) for _ in closes]
    line_char.add('收盘价', close_log)
    line_char.render_to_file('收盘价折线图-log.svg')


def draw_line(x_data, y_data, title, y_legend):
    xy_map = []
    for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _:_[0]):
        y_list = [v for _, v in y]
        xy_map.append([x, sum(y_list) / len(y_list)])
    
    x_unique, y_mean = [*zip(*xy_map)]
    line_chart = pygal.Line()
    line_chart.title = title
    line_chart.x_labels = x_unique
    line_chart.add(y_legend, y_mean)
    line_chart.render_to_file(title + ".svg")
    return line_chart

def unique_svg():
    json_result = read_json()
    dates = json_result[0]
    months = json_result[1]
    closes = json_result[4]

    idx_month = dates.index('2017-12-01')
    draw_line(
        months[:idx_month], closes[:idx_month], '收盘价月日均值', '月日均值')

    weeks = json_result[2]
    idx_week = dates.index('2017-12-11')
    draw_line(
        weeks[1:idx_week], closes[1:idx_week], '收盘价周日均值', '周日均值')
    
    weekdays = json_result[3]
    idx_week = dates.index('2017-12-11')
    wd = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday', 'Saturday', 'Sunday']
    weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
    line_chart_weekday = draw_line(
        weekdays_int, closes[1:idx_week], '收盘价星期均值（¥）', '星期均值')
    line_chart_weekday.x_labels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    line_chart_weekday.render_to_file('收盘价星期均值（¥）.svg')

if __name__ == "__main__":
    # btc_json()

    # btc_json_log10()

    unique_svg()
